package evostar.createMuchUser;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import evostar.model.User;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

public class UserUtil {

    private static void createUser(int count) throws Exception{
        List<User> users = new ArrayList<User>(count);
        //生成用户
        for(int i=0;i<count;i++) {
            User user = new User();
            user.setId(1000+i);
            user.setName("user"+user.getId());
            user.setSalt("abced");
            user.setPassword(DigestUtils.md5Hex("123456" + user.getSalt()));
            users.add(user);
        }
        System.out.println("create user.....");
        //登录，生成token
        String urlString = "http://localhost:8888/login/verify";
        File file = new File("E:/System/Desktop/tokens.txt");
        if(file.exists()) {
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        file.createNewFile();
        raf.seek(0);
        for(int i=0;i<users.size();i++) {
            User user = users.get(i);
            insert(user);
            URL url = new URL(urlString);
            HttpURLConnection co = (HttpURLConnection) url.openConnection();
            co.setRequestMethod("POST");
            co.setDoOutput(true);
            OutputStream out = co.getOutputStream();
            String params = "username=user"+user.getId()+"&password=123456";
            System.out.println(params);
            out.write(params.getBytes());
            out.flush();
            InputStream inputStream = co.getInputStream();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte buff[] = new byte[1024];
            int len = 0;
            while((len = inputStream.read(buff)) >= 0) {
                bout.write(buff, 0 ,len);
            }
            inputStream.close();
            bout.close();
            String response = new String(bout.toByteArray());
            JSONObject jo = JSON.parseObject(response);
            System.out.println(jo);
            String token = jo.getString("data");
            System.out.println("create token : " + user.getId());

            String row = user.getId()+","+token;
            raf.seek(raf.length());
            raf.write(row.getBytes());
            raf.write("\r\n".getBytes());
            System.out.println("write to file : " + user.getId());
        }
        raf.close();

        System.out.println("over");
    }
    public static void insert(User user) throws Exception {
        //		//插入数据库
        DBUtil dbUtil = new DBUtil();
        Connection conn = dbUtil.getConn();
        String sql = "insert into user(id, name, salt, password) values (?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, user.getId());
        pstmt.setString(2, user.getName());
        pstmt.setString(3, user.getSalt());
        pstmt.setString(4, user.getPassword());
        pstmt.addBatch();
        pstmt.executeBatch();
        pstmt.close();
        conn.close();
        System.out.println("insert to db");
    }

    public static void main(String[] args)throws Exception {
        createUser(1000);
    }
}
